package array;

import java.util.Arrays;

public class MultiArray {

    public static int[][] array = {{1,3},{5,7},{12,5,78,89}};

    public static void sparseArray() {
        //创建9*9数组
        int[][] array = new int[9][9];
        array[5][5] = 1;
        array[3][4] = 2;
        for (int[] ai : array) {
            for (int i : ai) {
                System.out.print(i+" ");
            }
            System.out.println();
        }

        //转化稀疏数组
        int sum = 0;
        for (int[] ints : array) {
            for (int anInt : ints) {
                if (anInt!=0) {
                    sum++;
                }
            }
        }
        System.out.println("有效值个数:"+sum);

        int[][] sparse = new int[sum+1][3];
        sparse[0][0] = 9;
        sparse[0][1] = 9;
        sparse[0][2] = sum;

        int count = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j]!=0) {
                    count++;
                    sparse[count][0] = i;
                    sparse[count][1] = j;
                    sparse[count][2] = array[i][j];
                }
            }
        }
        for (int[] ints : sparse) {
            System.out.println(Arrays.toString(ints));
        }

        //稀疏数组还原
        int[][] res = new int[sparse[0][0]][sparse[0][1]];
        for (int i = 1; i < sparse.length; i++) {
            res[sparse[i][0]][sparse[i][1]] = sparse[i][2];
        }
        for (int[] ai : res) {
            for (int i : ai) {
                System.out.print(i+" ");
            }
            System.out.println();
        }
    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            if (i==0) {
                System.out.print("["+array[i]+",");
            }else if (i==array.length-1) {
                System.out.println(array[i]+"]");
            }else {
                System.out.print(array[i]+",");
            }
        }
    }

    public static void bubbleSort(int[] array) {
        boolean flag = true;
        int temp = 0;
        for (int i = 0;i < array.length - 1;i++) {
            for (int j = 0;j < array.length - i - 1;j++) {
                if (array[j] > array[j+1]) {
                    temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = temp;
                    flag = false;
                }
            }
            if (flag) {
                break;
            }
        }
    }

    public static void main(String[] args) {
        //System.out.println(array[1].length);
        //System.out.println(array.length);
        MultiArray multiArray = new MultiArray();
        int[] a = {1,4,5,8,543,34,21,3};
        //比较"不用"和"不能"
        printArray(a);
        printArray(a);

        //排序
        bubbleSort(a);
        printArray(a);
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        Arrays.fill(a,0);
        System.out.println(Arrays.toString(a));

        //稀疏数组
        sparseArray();
    }
}
